$(function(){
	//首页加载数据
	searchOrder(AddDays(new Date(),7,'day').Format('yyyy-MM-dd'));
	//时间到期限制
	endTimeCheck();
});
function endTimeCheck(){
	var $search = $(".filt_options select");
	$search.on("change",function(){
		var $this = $(this),
			txt = $this.val();
		searchOrder(AddDays(new Date(),txt,'day').Format('yyyy-MM-dd'));
	});
}
//查询订单数据
function searchOrder(overTime) {
	var params = {
		overTime : overTime
	};
	
	$.ajax({
		type : "post",
		url : "/myservice/order/renewManage/queryList",
		async : false,
		data : params,
		dataType : "json",
		success : function(data){
			$('#renewList').empty();
			//新增内容
			var tb_content = '';
			if(data.orders.length > 0) {
				$.each(data.orders,function(index,order){
					tb_content += '<tr>'
						 	   + '<td data-id="'+order.orderId+'">'+order.orderNum+'</td>' 
							   + '<td>'+order.goodsName+'</td>'
							   + '<td>'+(parseFloat(order.payMoney).toFixed(2))+'</td>'
							   + '<td class="time">'+toDate(order.overTime,"yyyy-MM-dd hh:mm")+'</td>'
							   + '<td>'+checkIsDredge(order.isDredge)+'</td>'
							   + '<td>'+checkDoState(order.orderId,order.isDredge)+'</td>'
							   + '</tr>';
				})
			}else {
				tb_content += '<tr><td colspan="6">没有符合条件的订单，请尝试其他搜索条件。</td></tr>';
			}
			$('#renewList').append(tb_content);
		}
	});
}

//判断操作显示
function checkDoState(orderId,isDredge) {
	var str = '';
	if(isDredge == null || isDredge == '' || isDredge == 0){
		str = '<a href="javascript:void(0)" class="uc-button" onclick="dredge('+orderId+')">开通</a>';
	}else{
		str = '<a href="javascript:void(0)" class="uc-button" onclick="renew('+orderId+')">续费</a>';
	}
	return str;
}

//判断是否开通
function checkIsDredge(isDredge) {
	var str = '';
	if(isDredge == null || isDredge == '' || isDredge == 0){
		str = '未开通';
	}else{
		str = '已开通';
	}
	return str;
}


//判断购买类型
function checkIsFree(isFree,buyType) {
	var str = '';
	isFree = $.trim(isFree);
	if(isFree == null || isFree==''){
		str = '&nbsp;';
	}else{
		if(isFree == 0) {
			if(buyType == 0) {
				str = '购买';
			}else if(buyType == 0) {
				str = '续费';
			}else {
				str = '升级';
			}
		}else if(isFree == 1) {
			str = '试用';
		}
	}
	return str;
}

//直接查询某页
function ajax2Page(tbs,orderState) {
	searchOrder(tbs,orderState,$('#currPage'+tbs).val());
}

//向上翻页
function ajaxPageUp(tbs,orderState,page) {
	if(page == 1) {
		BSTYUN.dialog.warn("已经是第一页");
		return;
	}else {
		var num = parseInt(page) - 1;
		searchOrder(tbs,orderState,num);
	}
}

//向下翻页
function ajaxPageDown(tbs,orderState,page) {
	if(page == $("#pageCount"+tbs).val()) {
		BSTYUN.dialog.warn("已经是最后一页");
		return;
	}else {
		var num = parseInt(page) + 1;
		searchOrder(tbs,orderState,num);
	}
}

//续费
function renew(orderId){
	//校验是否可以续费
	$.ajax({
		type : "post",
		url : "/myservice/order/checkOrderRenew?orderId="+orderId,
		async : false,
		dataType : "json",
		success : function(data) {
			if(data.isSuccess) {
				window.open('/myservice/order/toRenewPage?orderId='+orderId);
			}else {
				BSTYUN.dialog.warn(data.str);
			}
		}
	})
}

//开通应用
function dredge(orderId){
	$.ajax({
		type : "post",
		url : "/eos/dredgeApply?orderId="+orderId,
		dataType : "json",
		beforeSend : function() {
			var loading = function(html,callback){
		        var dialog = null;
		        dialog = $.dialog({
		            id: 'loading',
		            fixed:true,
		            title:false,
		            lock:true,
		            padding:'10px 20px',
		            content: '<div class="loading-tip">'+html+'</div>'
		        });
		    }
			loading('<span style="color:red;font-size:16px;">开通正在进行,时长约为1分钟！请耐心等待！<br>开通成功后，页面会自动刷新！</span>');
		},
		success : function(data){
			BSTYUN.dialog.close("loading");
			if(data) {
				searchOrder();
			}else {
				BSTYUN.dialog.warn("网络异常，开通失败，请重试！");
			}
		},
		error : function() {
			BSTYUN.dialog.close("loading");
			BSTYUN.dialog.warn("网络错误，请稍后重试！");
		}
	})
}